package com.cn.wanxi.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class GetSqlSession {
    /**
     * 开启缓存
     *
     * @return
     */
    public static SqlSession getSqlSession() {
        SqlSession sqlSession = null;
        String resource = "druid.xml";
        SqlSessionFactory sqlSessionFactory = null;//开启工厂，一个工厂可以放多个SqlSession

        try {
// 打开资源文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
// 开启sqlsession
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            if (sqlSession == null) {
                sqlSession = sqlSessionFactory.openSession(true);//所有的的sqlsession对象都是factory.openSession（）得到的

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sqlSession;
    }

}
